package com.base.self.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.common.SelfRuntimeException;
import com.base.self.dto.commodityCallPlanDetail.CommodityCallPlanDetailWebAddDTO;
import com.base.self.dto.commodityCallPlanDetail.CommodityCallPlanDetailWebDelDTO;
import com.base.self.dto.commodityCallPlanDetail.CommodityCallPlanDetailWebListDTO;
import com.base.self.dto.commodityCallPlanDetail.CommodityCallPlanDetailWebUpdateDTO;
import com.base.self.entity.CommodityCallPlanDetail;
import com.base.self.mapper.CommodityCallPlanDetailMapper;
import com.base.self.service.ICommodityCallPlanDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Optional;

/**
 * 话费礼包详情
 *
 * @author XiFYuW
 * @since 2020-12-20
 */
@Service
public class CommodityCallPlanDetailServiceImpl extends ServiceImpl<CommodityCallPlanDetailMapper, CommodityCallPlanDetail> implements ICommodityCallPlanDetailService {

    private final CommodityCallPlanDetailMapper commodityCallPlanDetailMapper;

    public CommodityCallPlanDetailServiceImpl(CommodityCallPlanDetailMapper commodityCallPlanDetailMapper) {
        this.commodityCallPlanDetailMapper = commodityCallPlanDetailMapper;
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseResult commodityCallPlanDetailWebList(CommodityCallPlanDetailWebListDTO commodityCallPlanDetailWebListDTO) {
        final Map<String, Object> data = PageUtils.getDateMap(() -> page(PageUtils.getPage(
                new Page<>(), commodityCallPlanDetailWebListDTO.getOffset(), commodityCallPlanDetailWebListDTO.getLimit()),
                new LambdaQueryWrapper<CommodityCallPlanDetail>()
                        .eq(CommodityCallPlanDetail::getCallPlan, commodityCallPlanDetailWebListDTO.getCallPlan())
                        .eq(CommodityCallPlanDetail::getIsDel, 0)
                        .orderByDesc(CommodityCallPlanDetail::getCreateTime)));
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }

    @Override
    @Transactional
    public ResponseResult commodityCallPlanDetailWebAdd(CommodityCallPlanDetailWebAddDTO commodityCallPlanDetailWebAddDTO) {
        final CommodityCallPlanDetail commodityCallPlanDetail = CommodityCallPlanDetail
                .builder()
                .createTime(DateUtil.date())
                .planName(commodityCallPlanDetailWebAddDTO.getPlanName())
                .planPrice(commodityCallPlanDetailWebAddDTO.getPlanPrice())
                .planPic(commodityCallPlanDetailWebAddDTO.getPlanPic())
                .callPlan(commodityCallPlanDetailWebAddDTO.getCallPlan())
                .build();
        save(commodityCallPlanDetail);
        return ResponseResultUtils.getResponseResultS("添加成功");
    }

    @Override
    @Transactional
    public ResponseResult commodityCallPlanDetailWebUpdate(CommodityCallPlanDetailWebUpdateDTO commodityCallPlanDetailWebUpdateDTO) {
        final CommodityCallPlanDetail commodityCallPlanDetail = commodityCallPlanDetailMapper.selectById(commodityCallPlanDetailWebUpdateDTO.getId());
        Optional.ofNullable(commodityCallPlanDetail).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("话费礼包详情错误")));
        commodityCallPlanDetail.setUpdateTime(DateUtil.date());
        commodityCallPlanDetail.setPlanName(commodityCallPlanDetailWebUpdateDTO.getPlanName());
        commodityCallPlanDetail.setPlanPic(commodityCallPlanDetailWebUpdateDTO.getPlanPic());
        commodityCallPlanDetail.setPlanPrice(commodityCallPlanDetailWebUpdateDTO.getPlanPrice());
        updateById(commodityCallPlanDetail);
        return ResponseResultUtils.getResponseResultS("修改成功");
    }

    @Override
    @Transactional
    public ResponseResult commodityCallPlanDetailWebDelete(CommodityCallPlanDetailWebDelDTO commodityCallPlanDetailWebDelDTO) {
        final CommodityCallPlanDetail commodityCallPlanDetail = commodityCallPlanDetailMapper.selectById(commodityCallPlanDetailWebDelDTO.getId());
        Optional.ofNullable(commodityCallPlanDetail).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("话费礼详情错误")));
        commodityCallPlanDetail.setUpdateTime(DateUtil.date());
        commodityCallPlanDetail.setIsDel(1);
        updateById(commodityCallPlanDetail);
        return ResponseResultUtils.getResponseResultS("删除成功");
    }
}
